package com.sicau.socialpracticesystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sicau.socialpracticesystem.common.constant.UrlConstants;
import com.sicau.socialpracticesystem.common.returning.ResultData;
import com.sicau.socialpracticesystem.config.aop.ValidateAspect;
import com.sicau.socialpracticesystem.entity.param.PageParam;
import com.sicau.socialpracticesystem.entity.param.grade.GradeAddParam;
import com.sicau.socialpracticesystem.entity.param.grade.GradeUpdateParam;
import com.sicau.socialpracticesystem.entity.po.Grade;
import com.sicau.socialpracticesystem.entity.vo.PageDataVO;
import com.sicau.socialpracticesystem.service.GradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 年级表 前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@RestController
@PreAuthorize("isAuthenticated()")
@Api(tags = "年级相关接口")
@RequestMapping(UrlConstants.GRADE_PREFIX)
public class GradeController {


    @Resource
    private GradeService gradeService;

    @PostMapping
    @ApiOperation("添加一个年级")
    @ValidateAspect.ParamValidate
    public ResultData<Object> addGrade(@RequestBody @Valid GradeAddParam gradeAddParam,
                                       @ApiParam(hidden = true)
                                       @SuppressWarnings("unused") BindingResult bindingResult) {
        Grade savedGrade = gradeService.getOne(new LambdaQueryWrapper<Grade>()
                .eq(Grade::getGrade, gradeAddParam.getGrade()));
        if (savedGrade != null) {
            return new ResultData<>().FAILED().setResultIns("该年级已经存在");
        }
        return gradeService.save(new Grade(gradeAddParam.getGrade()))
                ? new ResultData<>().OK()
                : new ResultData<>().FAILED();
    }

    @DeleteMapping
    @ApiOperation("删除一个或多个年级")
    public ResultData<Object> removeGrades(@RequestParam("ids") ArrayList<Integer> ids) {
        return gradeService.removeByIds(ids)
                ? new ResultData<>().OK()
                : new ResultData<>().FAILED().setResultIns("部分或全部数据删除失败, 可能有的数据已经被删除了");
    }

    @GetMapping("page")
    @ApiOperation("查询所有年级(带分页)")
    public ResultData<PageDataVO<Grade>> getGradeByPage(PageParam pageParam) {
        return new ResultData<PageDataVO<Grade>>().OK().setObj(gradeService.queryGradesByPage(pageParam));
    }

    @GetMapping
    @ApiOperation("查询所有年级")
    public ResultData<List<Grade>> getGrade() {
        return new ResultData<List<Grade>>().OK().setObj(gradeService.queryGrades());
    }

    @PutMapping("{id}")
    @ApiOperation("修改年级")
    public ResultData<Object> updateGrade(@PathVariable("id") Integer id,
                                          @RequestBody @Valid GradeUpdateParam gradeUpdateParam,
                                          @ApiParam(hidden = true)
                                          @SuppressWarnings("unused") BindingResult bindingResult) {
        Grade grade = gradeService.getById(id);
        if (grade == null) {
            return new ResultData<>().FAILED().setResultIns("年级不存在");
        } else if (grade.getGrade().equals(gradeUpdateParam.getGrade())) {
            return new ResultData<>().FAILED().setResultIns("该年级已经存在");
        }
        return gradeService.updateById(grade.setGrade(gradeUpdateParam.getGrade()))
                ? new ResultData<>().OK()
                : new ResultData<>().FAILED();
    }


}

